package com.topscore.uke.mvp.presenters;

import com.kingja.loadsir.core.LoadSir;
import com.topscore.uke.entities.StuffListEntity;
import com.topscore.uke.mvp.contractors.GetStuffListContractor;
import com.topscore.uke.mvp.model.GetStuffListModel;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.network.request.GetStuffListRequest;
import com.topscore.uke.ui.loadsir_callback.ErrorCallBack;
import com.topscore.uke.ui.loadsir_callback.LoadingCallBack;

/**
 * Created by 71468 on 2018/3/14.
 */

public class GetStuffListPresenter implements GetStuffListContractor, GetStuffListContractor.Presenter {
    private View mView;
    private GetStuffListRequest mRequest;
    private int mCurrentPage = 1;
    private Model mModel;

    public GetStuffListPresenter(View mView) {
        this.mView = mView;
        this.mModel = new GetStuffListModel();
        mRequest = new GetStuffListRequest();
        mRequest.setPageSize(10);
        mRequest.setPage(mCurrentPage);
    }

    @Override
    public void getStuffByClassId(int classId) {
        mView.getLoadService().showCallback(LoadingCallBack.class);
        mCurrentPage = 1;
        mRequest.setCategory(classId);
        mRequest.setPage(mCurrentPage);
        mModel.getStuffList(mRequest, new CallBack() {
            @Override
            public void onGetDataSuccess(BaseResponse<StuffListEntity> response) {
                mView.getLoadService().showSuccess();
                mView.getRecyclerAdapter().setNewData(response.getData().getContent());
                if (response.getData().getContent().size() < mRequest.getPageSize()) {
                    mView.getRecyclerAdapter().loadMoreComplete();
                    mView.getRecyclerAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
//                mView.onLoadFinished();
            }
        });
    }

    @Override
    public void getStuffByKeyWords(String keyWord) {
        mCurrentPage = 1;
        mRequest.setPage(mCurrentPage);
        mRequest.setKeyword(keyWord);
        mModel.getStuffList(mRequest, new CallBack() {
            @Override
            public void onGetDataSuccess(BaseResponse<StuffListEntity> response) {
                mView.getRecyclerAdapter().setNewData(response.getData().getContent());
                if (response.getData().getContent().size() < mRequest.getPageSize()) {
                    mView.getRecyclerAdapter().loadMoreComplete();
                    mView.getRecyclerAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
    }

    @Override
    public void getStuffByCategory(int category) {
        mCurrentPage = 1;
        mRequest.setPage(mCurrentPage);
        mModel.getStuffList(mRequest, new CallBack() {
            @Override
            public void onGetDataSuccess(BaseResponse<StuffListEntity> response) {
                mView.getRecyclerAdapter().setNewData(response.getData().getContent());
                if (response.getData().getContent().size() < mRequest.getPageSize()) {
                    mView.getRecyclerAdapter().loadMoreComplete();
                    mView.getRecyclerAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
    }

    @Override
    public void getStuffBySort(int sort) {
        mCurrentPage = 1;
        mRequest.setPage(mCurrentPage);
        mRequest.setSort(sort);
        mModel.getStuffList(mRequest, new CallBack() {
            @Override
            public void onGetDataSuccess(BaseResponse<StuffListEntity> response) {
                mView.getRecyclerAdapter().setNewData(response.getData().getContent());
                if (response.getData().getContent().size() < mRequest.getPageSize()) {
                    mView.getRecyclerAdapter().loadMoreComplete();
                    mView.getRecyclerAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
    }

    @Override
    public void getStuffByBrand(String brand) {
        mRequest.setBrand(brand);
        mModel.getStuffList(mRequest, new CallBack() {
            @Override
            public void onGetDataSuccess(BaseResponse<StuffListEntity> response) {
                mView.getRecyclerAdapter().setNewData(response.getData().getContent());
                if (response.getData().getContent().size() < mRequest.getPageSize()) {
                    mView.getRecyclerAdapter().loadMoreComplete();
                    mView.getRecyclerAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
    }

    @Override
    public void getNextPage() {
        int page = mCurrentPage + 1;
        mRequest.setPage(page);
        mModel.getStuffList(mRequest, new CallBack() {
            @Override
            public void onGetDataSuccess(BaseResponse<StuffListEntity> response) {
                if (!response.getData().getContent().isEmpty()){

                    mCurrentPage = page;
                    mView.getRecyclerAdapter().addData(response.getData().getContent());
                    if (response.getData().getContent().size() < mRequest.getPageSize()) {
                        mView.getRecyclerAdapter().loadMoreComplete();
                        mView.getRecyclerAdapter().loadMoreEnd();
                    }
                }else {
                    mView.getRecyclerAdapter().loadMoreComplete();
                    mView.getRecyclerAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.getRecyclerAdapter().loadMoreFail();
            }

            @Override
            public void onComplete() {
                mView.getRecyclerAdapter().loadMoreComplete();
            }
        });
    }
}
